<?php 
session_cache_expire(999999999);
if(!isset($_SESSION)) 
{ 
	session_start(); 
}
$lang = $_SESSION['lang'];
if (empty($lang) || $lang == "EN") {
	include "../value/value_en.php";
} else if($lang == "VN") {
	include "../value/value_vn.php";
}
else {
	include "../value/value_kr.php";
}
include '../operations/connection.php';	

$month=$_REQUEST['month'];
$year=$_REQUEST['year'];

$sqlCategory=mysql_query("SELECT * FROM category c WHERE c.isDelete=0 ORDER BY DAY(c.dateStart)");

$mang=array();

//echo $month.'-'.$year;

while ($row = mysql_fetch_array($sqlCategory)){
	
	$d=$row['dateStart'];
	$r=$row['regular'];
	$i=0;


	$mang1[]=array($row['id'],$row['name'],$d,$r,$row['isDelete'],$row['method'],$row['payment']);
	//echo strtotime($d)."-".strtotime($year.'-'.$month.'-'.date("d")).'<br>';
	//if($row['isDelete']==0){
	//if(strtotime($d)<strtotime($year.'-'.$month.'-'.date("d")))
	//if( intval(date('m',strtotime($d))) <= intval($month) && intval(date('Y',strtotime($d))) <= intval($year) )
	//echo $d;
	//$date=date("".intval($year)."-".intval($month)."-01");
	while ($i>-1){
			//if( intval(date('m',strtotime($d." +".($r*$i)." month"))) > intval($month) && intval(date('Y',strtotime($d." +".($r*$i)." month"))) > intval($year) ){
			//	$i=-3;
			//}
			//echo $d;
			//echo intval(date('m',strtotime($d." +".($r*$i)." month"))).'-'.intval(date('Y',strtotime($d." +".($r*$i)." month"))).'     ';
			if( intval(date('m',strtotime($d." +".($r*$i)." month"))) == intval($month) && intval(date('Y',strtotime($d." +".($r*$i)." month"))) == intval($year) ){
				$d0=strtotime($d." +".($r*$i)." month");
	 			$d1= date("Y-m-d",$d0);
	 	
	 			$d10= date("m",$d0);
	 			$d11= date("Y",$d0);
	 			$kq=mysql_query("SELECT l.money,l.status,l.note,l.method FROM LogMoney l WHERE l.idcategory=".$row['id']." AND MONTH(l.datetime)='$d10' AND YEAR(l.datetime)='$d11'");
	 			
	 			//echo "SELECT l.money,l.status,l.note FROM LogMoney l WHERE l.idcategory=".$row['id']." AND MONTH(l.datetime)='$d10' AND YEAR(l.datetime)='$d11';"."<br>";
	 			$money="";
	 			$status=0;
	 			if(mysql_num_rows($kq)>0){
		 			while ($row1=mysql_fetch_array($kq)){
		 				$money=number_format($row1['money']);
		 				$status=$row1['status'];
		 				$method=$row1['method'];
		 				$note=$row1['note'];
		 				$mang[]=array($row['id'],$row['name'],$d1,$money,$status,$method,$note);
		 			}
	 			
	 			}else{
	 				
	 				$mang[]=array($row['id'],$row['name'],$d1,$row['payment'],0,$row['method'],"");
	 				$a=split('-',$d1);
	 				$kq = mysql_query("SELECT * from LogMoney l where l.idcategory =".$row['id']." AND MONTH(l.datetime)='".$a[1]."' AND YEAR(l.datetime)='".$a[0]."'");
					if(mysql_num_rows($kq)<1)
						mysql_query("INSERT INTO `LogMoney` (`idcategory`, `datetime`, `money`,`status`,`method`,`note`) VALUES ('".$row['id']."', '".$d1."', '".$row['payment']."', '0','".$row['method']."','')");
	 			}
	 			$i=-3;
	 			
			}
			if(intval(date('Y',strtotime($d." +".($r*$i)." month"))) > intval($year)){
				$i=-3;
			}
			
		//}
		//if( strtotime($d." +".($r*$i)." month")>strtotime($date)){
		//	$i=-3;
		//}
		$i++;
		
	}
	//echo  "<br>";
	
}
$_SESSION['inputMoneys']=$mang;
?>
<table cellpadding="0" cellspacing="1" border="0" class="ftable1" style="width: 100%;margin:auto;">
			<tr align='center'>
				<th style="width:30px;"><?=$l_no?></th>
				<th style="width:190px;"><?=$l_pay_category?></th>
				<th style="width:110px;"><?=$l_pay_time?></th>
				<th style="width:110px;"><?=$l_pay_money?></th>
				<th style="width:110px;"><?=$l_methodsPayment_contract?></th>
				<th style="width:110px;"><?=$l_note?></th>
				<th><?=$l_status?></th>
			</tr>
			
		<?php 
		$k=1;
		foreach ($mang as $value => $giatri){
		
			?>
							<tr>
								<td><?=$k?></td>
								<td class="tdfirst" style="text-align: left;"><?php echo $giatri[1];?></td>
								<td style="width: 100px;" ><input type="text" value="<?php echo $giatri['2'];?>" class="in_testfield" style="width:100px;float:left;height:24px;text-align: right; padding-right: 5px" readonly="readonly" disabled="disabled" /></td>
								<td ><input type="text" id="money<?=$k?>" name="money<?=$k?>" value="<?php echo $giatri[3];?>" onchange="chMoney(this.value,<?=$giatri[0]?>)" onkeyup="chMoney(this.value,<?=$giatri[0]?>)" <?php if($giatri[4]==1) echo 'disabled="disabled"';?>  class="in_testfield" style="width:100px;float:left;height:24px;text-align: right; padding-right: 5px"/></td>
								<td >
									<!-- <input type="text" id="method<?=$k?>" name="method<?=$k?>" value="<?php echo $l_method?>" disabled="disabled" style="width:140px;float:left;height:24px;text-align: left; "/>-->
									<select  id="method<?=$k?>" name="method[]" onchange="chmethod1(this.value,<?=$giatri[0]?>)" onkeyup="chmethod1(this.value,<?=$giatri[0]?>)" style="height: 24px;" <?php if($giatri[4]==1) echo 'disabled="disabled"';?>>
										<?php for ($i=0;$i<=5;$i++){
											$l_method="";
											if($i==0)
												$l_method=$l_method0;
											if($i==1)
												$l_method=$l_method1;
											if($i==2)
												$l_method=$l_method2;
											if($i==3)
												$l_method=$l_method3;
											if($i==4)
												$l_method=$l_method4;
											if($i==5)
												$l_method=$l_method5;
													
											if($giatri[5]==$i){
											?>
										<option value="<?=$i?>" selected="selected"><?=$l_method?></option>
											<?php }else{?>
										<option value="<?=$i?>"><?=$l_method?></option>
										<?php }
											}?>
									</select>
								</td>
								<td ><input type="text" id="note<?=$k?>" name="note<?=$k?>" value="<?php echo $giatri[6];?>" onchange="chNote(this.value,<?=$giatri[0]?>)" onkeyup="chNote(this.value,<?=$giatri[0]?>)" <?php if($giatri[4]==1) echo 'disabled="disabled"';?>  class="in_testfield" style="width:140px;float:left;height:24px;"/></td>
								<td ><?php if($giatri[4]==0){?>
									<img title="<?=$l_pay_status1?>" src="images/not_yet.png" onclick="chActive(<?=$giatri[0]?>,0)">
									<?php }else{?>
									<img title="<?=$l_pay_status2?>" src="images/check.png" onclick="chActive(<?=$giatri[0]?>,1)">
									<?php }?></td>
							</tr>
							
							<?php 
							$k++;
		}
		?>
			
		</table>
<?php mysql_close($connection);?>